package org.example;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentTest {
    public static void main(String[] args) {
        ArrayList<Student>  list = new ArrayList<>();
        Scanner sc=new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            Student s=new Student();
            System.out.println("请输入学生名字：");
            String name=sc.next();
            s.setName(name);
            System.out.println("请输入学生年龄：");
            int age = sc.nextInt();
            s.setAge(age);
            System.out.println("请输入学生Id:");
            int id=sc.nextInt();
            s.setId(id);

            list.add(s);

        }

        for (int i = 0; i < list.size(); i++) {
            //获取集合中的每一个学生对象
            Student stu =list.get(i);
            System.out.println("学生名字："+stu.getName()+" "+"学生年龄："+stu.getAge());
        }

        System.out.println("请输入要查找的学生Id:");
        int id=sc.nextInt();
        boolean flag=contains(list,id);
        System.out.println(flag);
    }
    public static boolean contains(ArrayList<Student>list,int id){
       /* for (int i = 0; i < list.size(); i++) {
            Student stu=list.get(i);
            if (stu.getId()==id){
                return true;
            }
        }
        return false;*/
       if(getIndex(list,id)>=0){
           return true;
       }else{
           return false;
       }
    }
   public static int getIndex(ArrayList<Student>list,int id){
        for (int i = 0; i < list.size(); i++) {
            Student stu=list.get(i);
            if (stu.getId()==id){
                return i;
            }
        }
        return -1;
    }
}
